package com.ruoyi.gateway.filter;

import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.gateway.config.properties.BlackProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@Slf4j
public class GlobalFilterConfig {

    @Resource
    private BlackProperties config;

    @Bean
    public GlobalFilter blackListGlobalFilter() {
        log.info("黑名单过滤器初始化完成，config.getBlacklistUrl()={}",config.getBlacklistUrl());

        return (exchange, chain) -> {
            // 获取请求路径
            String path = exchange.getRequest().getURI().getPath();

            // 调用你原来的逻辑判断是否匹配黑名单
            if (config.matchBlacklist(path)) {
                return ServletUtils.webFluxResponseWriter(exchange.getResponse(), "请求地址不允许访问");
            }

            return chain.filter(exchange);
        };
    }
}
